<?php



function _sendxml_service($nid, $sid, $xml_data) {
  global $user;
  $service = INTERACT_GATEWAY_API . "/relocom/enquiry";
  $data = 'requestXML='.urlencode($xml_data);
  $method = 'post';
  $api_result = call_workflow_service_api($service, $data, $method, TRUE);
  watchdog('api', '%api', array('%api' => $api_result), WATCHDOG_ERROR);
  $rs = array();
  $http_code = isset($api_result['http_code'])? $api_result['http_code'] : '';
  if(isset($http_code) && $http_code===200 ){
      $xml = simplexml_load_string($api_result['body']);
      if($xml->statusCode == '200'){
        
        $rs['statusCode'] = 200;
        //$other_msg = isset($xml->DataSet->DataObject->newEnquiryId) ? (string)$xml->DataSet->DataObject->newEnquiryId : '';
        $rs['statusMessage'] = (string)$xml->statusMessage ;
        $rs['sourceKey'] = (string)$xml->DataSet->DataObject->sourceKey;
        $rs['SourceDatabasePath'] = (string)$xml->DataSet->DataObject->SourceDatabasePath;
        $rs['sourceServer'] = (string)$xml->DataSet->DataObject->sourceServer;
        $rs['enquiryNumber'] = (string)$xml->DataSet->DataObject->enquiryNumber;
        $rs['newEnquiryId'] = (string)$xml->DataSet->DataObject->newEnquiryId;
        _set_sendxml_status($nid, $sid, 1,$rs['enquiryNumber'],$rs['newEnquiryId']);
      }else{
        $rs['statusCode'] = (string)$xml->statusCode;
        $rs['statusMessage'] = (string)$xml->statusMessage ;
        $rs['sourceKey'] = (string)$xml->DataSet->DataObject->sourceKey;
        $rs['SourceDatabasePath'] = (string)$xml->DataSet->DataObject->SourceDatabasePath;
        $rs['sourceServer'] = (string)$xml->DataSet->DataObject->sourceServer;
        $rs['enquiryNumber'] = (string)$xml->DataSet->DataObject->enquiryNumber;
        $rs['newEnquiryId'] = (string)$xml->DataSet->DataObject->newEnquiryId;
        _set_sendxml_status($nid, $sid, 0,$rs['enquiryNumber'],$rs['newEnquiryId']);
      }
  }
  else{
    $rs['statusCode'] = '500?'.$api_result['http_code'] ;
    $rs['statusMessage'] = t('Network Problem. Please try again later.') ;
    _set_sendxml_status($nid, $sid, 0);
  }
  
  return $rs;
}



function _set_sendxml_status($nid, $sid, $status, $enquiryNumber, $code) {
  $data = array();
  $data['nid'] = $nid;
  $data['status'] = (int)$status;
  $data['created'] = REQUEST_TIME;
  $data['enquiry_number'] = $enquiryNumber;
  $data['ext_system_code'] = $code;
  $primary_keys = array(
    'sid' => $sid,
  );
  $query = db_merge('webform_submissions_sendxml')
    ->key($primary_keys)
    ->fields($data)
    ->execute();
}


function _get_sendxml_status($nid, $sid) {
  $query = db_select('webform_submissions_sendxml', 's');
  $query->fields('s', array('status'));
  $query->condition('s.nid', $nid);
  $query->condition('s.sid', $sid);
  $result = $query->execute()->fetchColumn(); //print_r($result);die;
  if(empty($result)){
    $result = 0 ;
  }
  return $result;
}


/*
 * In order to ajax request, action the "Synchronize to interact" button
 */
function synchronize_to_interact(){
  
  $nid = (int)$_POST['nid']; 
  $sid = (int)$_POST['sid'];

  $node = node_load($nid);
  $submission = webform_get_submission($node->webform['nid'], $sid);
  if(!$node || !$submission){
    $json = '{msg:"Parameter error!",status:500}';
    echo $json;
    exit;
  }
  
  $result = _create_submission_xml($node,$submission); //print_r($result);die;
  $results = _sendxml_service($nid, $sid, $result);
  $json = '';
  if ($results['statusCode'] == '200') {
    $json = '{msg:"'.$results['statusMessage'].'",status:200}';
  } else {
    $json = '{msg:"'.$results['statusMessage'].'",status:'.$results['statusCode'].'}';
  }
  echo $json;
  exit;
}
